package com.qingsong.mapper;

import com.github.pagehelper.PageInfo;
import com.qingsong.dto.PageDTO;
import com.qingsong.entity.School;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface SchoolMapper {
    @Insert("INSERT INTO rms_school (`title`,`college`,`major`,`clazz`,`address`,`info`,`deleted`,`created`,`updated`) " +
            "VALUES" +
            " (#{title},#{college},#{major},#{clazz},#{address},#{info},#{deleted},#{created},#{updated})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insert(School school);

    @Select("select t.id, t.title, t.college, t.major, t.clazz, t.address, t.info, " +
            "t.deleted, t.created, t.updated from rms_school t "+
            "where t.id = #{param1} and t.deleted = 0")
    School selectById(Long id);

    @Update("<script>" +
            "update rms_school " +
            "<set> " +
            "<if test='title != null'> title = #{title}, </if>" +
            "<if test='college != null'> college = #{college}, </if>" +
            "<if test='major != null'> major = #{major}, </if>" +
            "<if test='clazz != null'> clazz = #{clazz}, </if>" +
            "<if test='address != null'> address = #{address}, </if>" +
            "<if test='info != null'> info = #{info}, </if>" +
            "<if test='deleted != null'> deleted = #{deleted}, </if>" +
            "<if test='created != null'> created = #{created}, </if>" +
            "<if test='updated != null'> updated = #{updated}, </if>" +
            "</set> " +
            "where id = #{id}" +
            "</script>")
    int updateById(School school);

    @Update("<script>" +
            "update rms_school set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='id in (' close=')' separator=','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Update("update rms_school set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);

    @Select("select t.id, t.title, t.college, t.major, t.clazz, t.address, t.info, " +
            "t.deleted, t.created, t.updated from rms_school t "+
            "where t.deleted = 0")
    List<School> list();

    @Select("select t.id, t.title, t.college, t.major, t.clazz, t.address, t.info, " +
            "t.deleted, t.created, t.updated from rms_school t "+
            "where t.deleted = 0")
    PageInfo<School> page(PageDTO pageDTO);

    @Select("select t.id, t.title, t.college, t.major, t.clazz, t.address, t.info, " +
            "t.deleted, t.created, t.updated from rms_school t " +
            "where t.title = #{title} " +
            "and t.college = #{college} " +
            "and t.major = #{major} " +
            "and t.clazz = #{clazz} ")
    School selectByAllTitle(School school);

    @Delete("delete from rms_school where id = #{param1}")
    int deleteById(Long id);

    @Select("select t.id, t.title, t.college, t.major, t.clazz, t.address, t.info, " +
            "t.deleted, t.created, t.updated from rms_school t "+
            "where t.title = #{param1}")
    School selectByTitle(String title);

    @Select("select t.id, t.title, t.college, t.major, t.clazz, t.address, t.info, " +
            "t.deleted, t.created, t.updated from rms_school t "+
            "where t.deleted = 0 and t.title like concat('%', #{param1}, '%')")
    List<School> listLikeTitle(String title);
}
